package net.wofly.cashier.room.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.cashier.room.domain.Room;
import net.wofly.cashier.room.service.IRoomService;
import net.wofly.cashier.room.validator.RoomValidator;
import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;

/**
 *
 */
@Controller
@RequestMapping(value = "/api/room")
@Api(description = "房间管理接口")
public class RoomController extends BaseController {

    @Autowired
    IRoomService iRoomService;

    /**
     * 新增房间信息
     * <ul>
     * <li>验证employeeID用户权限</li>
     * </ul>
     *
     * @param room
     * @param employeeID
     */
    @ApiOperation(value = "新增房间信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "room", value = "房间实体", required = true, dataType = "Room"),
                               @ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String")})
    @RequestMapping(value = "/addRoom", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Room> addRoom(@RequestBody Room room, String employeeID) {
        ReturnBean<Room> returnBean;
        String errorMessage = RoomValidator.validateAddOrUpdateRoomParams(room, employeeID,false);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Room>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iRoomService.addRoom(room, employeeID);
        }
        return returnBean;
    }

    /**
     * 删除房间
     * <ul>
     * <li>验证employeeID用户权限</li>
     * </ul>
     * <ul>
     * <li></li>
     * </ul>
     *
     * @param roomID
     * @param employeeID
     */
    @ApiOperation(value = "删除房间")
    @ApiImplicitParams({@ApiImplicitParam(name = "roomID", value = "房间ID", required = true, dataType = "String"),
                               @ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String")})
    @RequestMapping(value = "/delRoom", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<String> delRoom(String roomID, String employeeID) {
        ReturnBean<String> returnBean;
        String errorMessage = RoomValidator.validateDelRoomParams(roomID, employeeID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<String>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iRoomService.delRoom(roomID, employeeID);
        }
        return returnBean;
    }

    /**
     * 一次性获取所有Room，不分页。根据状态来判断是否取全部
     * <ul>
     * <li>status=1 取所有有效的房间</li>
     * <li>status=0 取所有无效的房间</li>
     * <li>status=-1 取所有房间</li>
     * <li>验证employeeID用户权限</li>
     * </ul>
     *
     * @param status
     * @param employeeID
     */
    @ApiOperation(value = "一次性获取所有Room，不分页。根据状态来判断是否取全部。status=1 取所有有效的房间；status=0 取所有无效的房间；status=-1 取所有房间")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "房间状态，status=1 取所有有效的房间；status=0 取所有无效的房间；status=-1 取所有房间", required = true, allowableValues = "1,0,-1", dataType = "Integer"),
                               @ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String")})
    @RequestMapping(value = "/getAllRoom", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Room>> getAllRoom(@RequestParam(defaultValue = "1") Integer status, String employeeID) {
        ReturnBean<List<Room>> returnBean;
        String errorMessage = RoomValidator.validateGetAllRoomParams(employeeID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<Room>>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iRoomService.getAllRoom(status, employeeID);
        }
        return returnBean;
    }

    /**
     * 修改房间信息
     * <ul>
     * <li></li>
     * <li>验证employeeID用户权限</li>
     * </ul>
     *
     * @param room
     * @param employeeID
     */
    @ApiOperation(value = "修改房间信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "room", value = "房间实体", required = true, dataType = "Room"),
                               @ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String")})
    @RequestMapping(value = "/updateRoom", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<Room> updateRoom(@RequestBody Room room, String employeeID) {
        ReturnBean<Room> returnBean;
        String errorMessage = RoomValidator.validateAddOrUpdateRoomParams(room, employeeID,true);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<Room>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iRoomService.updateRoom(room, employeeID);
        }
        return returnBean;
    }

}
